package com.apes.fn.mdm.supplierConfiguration;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.product.model.Classify;
import com.apes.scm.rbac.model.Role;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierClassify.CONST_TABLE_NAME)
public class SupplierClassify extends BaseModel {

    public static final String CONST_TABLE_NAME = "mas_supplier_classify";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商分类")
    @Comment("编码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "supplier_susiness_scopee_id", referencedColumnName = "id")
    @Comment("供应商经营范围编码")
    private SupplierBusinessScope supplierBusinessScopeId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "classify_id", referencedColumnName = "id")
    @Comment("品类")
    private Classify classifyId;

    @ManyToMany(cascade = {CascadeType.DETACH}, fetch = FetchType.LAZY)
    @JoinTable(name = "mas_exclude_classify",
            joinColumns = {@JoinColumn(name = "supplier_classify_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "classify_id", referencedColumnName = "id")}
    )
    private List<Classify> excludeClassify = new ArrayList<>();

    // 以下字段给前端临时展示用
    @Transient
    private int levels;

    public int getLevels() {
        return levels = this.classifyId.getLevels();
    }
}
